package com.xin.beauty.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xin.beauty.listener.ProjectExcelListener;
import com.xin.beauty.pojo.entity.Project;
import com.xin.beauty.pojo.entity.Project;
import com.xin.beauty.mapper.ProjectMapper;
import com.xin.beauty.pojo.query.ProjectQuery;
import com.xin.beauty.pojo.vo.ProjectExcelVO;
import com.xin.beauty.service.IProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xin.beauty.util.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Xin
 * @since 2025-05-04
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {

    @Autowired
    private ProjectMapper projectMapper;
    @Override
    public IPage<Project> list(ProjectQuery projectQuery) {
        IPage<Project> page = new Page<>(projectQuery.getPage(), projectQuery.getLimit());
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(projectQuery.getName()), "name", projectQuery.getName());
        queryWrapper.like(!ObjectUtils.isEmpty(projectQuery.getId()), "id", projectQuery.getId());
//        queryWrapper.eq(!ObjectUtils.isEmpty(projectQuery.getDoctorId()), "doctor_id", projectQuery.getDoctorId());
        queryWrapper.between(!ObjectUtils.isEmpty(projectQuery.getBeginTime()) && !ObjectUtils.isEmpty(projectQuery.getEndTime()),
                "update_time", projectQuery.getBeginTime(), projectQuery.getEndTime());
        projectMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public List<Project> listNotAll() {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( "status", 1);
        List<Project> projectList = projectMapper.selectList(queryWrapper);
        return projectList;
    }



    @Override
    public void exportExcel(HttpServletResponse response) {
        List<Project> list = projectMapper.selectList(null);
        ArrayList<ProjectExcelVO> projectExcelVOList = new ArrayList<>();
        for (Project project : list) {
            ProjectExcelVO projectExcelVO = new ProjectExcelVO();
            BeanUtils.copyProperties(project, projectExcelVO);//new一个工具类 复制
            projectExcelVOList.add(projectExcelVO);//如果不做ProjectExcelVO这些就不用做了 不想破坏project实体类
        }
        ExcelUtil.exportExcel(response, projectExcelVOList, ProjectExcelVO.class, "project");
    }

    @Override
    public void importExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), ProjectExcelVO.class, new ProjectExcelListener(projectMapper)).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
